<?php
    function get_date_part($date,$part_to_return='')
    {
        $date_str = array();
        $date_str = explode('-',$date);
        if($part_to_return=='year'){
            return $date_str[0];
        }
        if($part_to_return=='month'){
            return $date_str[1];
        }
        if($part_to_return=='day'){
            return $date_str[2];
        }
    }
    function delete_pro_file($file_to_delete) {
        if (file_exists($file_to_delete)) {
            unlink($file_to_delete);
        }
    }

    function is_user($go_after_login=TRUE) {
        $CI = & get_instance();
        if ($CI->session->userdata('user_id')) {
            return true;
        }
        if ($go_after_login == TRUE) {
            redirect('accueil');
        }
        return FALSE;
    }

    function convertdate($string) {
        return implode('/', array_reverse(explode('-', $string)));
    }

    function convertdate2($string) {
        return implode('-', array_reverse(explode('/', $string)));
    }

    function delete_image($image_to_delete, $thumb_to_delete) {
        if (file_exists($image_to_delete)) {
            unlink($image_to_delete);
        }
        if (file_exists($thumb_to_delete)) {
            unlink($thumb_to_delete);
        }
    }

    function get_short_description($desription, $no_of_word) {
        $ret_val = '';
        $str = generate_pure_text($desription, '<p>', '</p>');
        $str_array = array();
        $str_array = explode(' ', $str);
        if ($no_of_word > count($str_array)) {
            return $str;
        }
        for ($i = 0; $i < $no_of_word; $i++) {
            $ret_val.=$str_array[$i] . ' ';
        }
        return $ret_val;
    }

    function generate_pure_text($html_text, $start_element, $end_element) {
        $pure_text = '';
        $str = array();
        $str = explode($start_element, $html_text);
        $first_process_text = '';
        for ($i = 0; $i < count($str); $i++) {
            $first_process_text.=$str[$i];
        }
        $str = explode($end_element, $first_process_text);
        for ($i = 0; $i < count($str); $i++) {
            $pure_text.=$str[$i];
        }
        return $pure_text;
    }

    function is_admin($go_after_login=TRUE) {
        $CI = & get_instance();
        if ($CI->session->userdata('adminid')) {
            return TRUE;
        }
        if ($go_after_login == TRUE) {
            redirect('admin/login');
        }
        return FALSE;
    }

    function is_super_admin($is_normal_admin=TRUE) {
        $CI = & get_instance();
        if ($CI->session->userdata('adminid') && $CI->session->userdata('level') == 1) {
            $admin_data = array
            (
            'email' => $CI->session->userdata('email'),
            'adminid' => $CI->session->userdata('adminid'),
            'level' => $CI->session->userdata('level'),
            );
            return $admin_data;
        }
        if ($is_normal_admin == TRUE) {
            redirect('admin/list_admin');
        }
        return FALSE;
    }

    function form_fckeditor($data = '', $value = '', $extra = '') {
        $CI = & get_instance();
        $fckeditor_basepath = $CI->config->item('fckeditor_basepath');
        require_once( $_SERVER["DOCUMENT_ROOT"] . $fckeditor_basepath . 'fckeditor.php' );
        $instanceName = ( is_array($data) && isset($data['name']) ) ? $data['name'] : $data;
        $fckeditor = new FCKeditor($instanceName);

        if ($fckeditor->IsCompatible()) {
            $fckeditor->Value = $value; //html_entity_decode($value);
            $fckeditor->BasePath = $fckeditor_basepath;
            if ($fckeditor_toolbarset = $CI->config->item('fckeditor_toolbarset_default'))
                $fckeditor->ToolbarSet = $fckeditor_toolbarset;

            if (is_array($data)) {
                if (isset($data['value']))
                    $fckeditor->Value = $data['value']; //html_entity_decode($data['value']);
                if (isset($data['basepath']))
                    $fckeditor->BasePath = $data['basepath'];
                if (isset($data['toolbarset']))
                    $fckeditor->ToolbarSet = $data['toolbarset'];
                if (isset($data['width']))
                    $fckeditor->Width = $data['width'];
                if (isset($data['height']))
                    $fckeditor->Height = $data['height'];
            }
            return $fckeditor->CreateHtml();
        }
        else {
            return form_textarea($data, $value, $extra);
        }
    }

    if (!function_exists('dropdown_data')) {

        function dropdown_data($setting) {
            $CI = & get_instance();
            if (!array_key_exists('order', $setting)) {
                $CI->db->order_by($setting['value_field'], 'asc');
            } else {
                $CI->db->order_by($setting['order']);
            }
            if (isset($setting['where'])) {
                $CI->db->where($setting['where']);
            }

            $query = $CI->db->get($setting['table_name']);
            $options = array();

            if (isset($setting['init_data'])) {
                $options[$setting['init_data']['key']] = $setting['init_data']['name'];
            }

            foreach ($query->result() as $row) {
                $options[$row->$setting['key_field']] = $row->$setting['value_field'];
            }
            return $options;
        }

    }
?>